var utils = require('../../utils/address.js');
var app = getApp()
Page({
  data: {
    motto: 'H111112222',
    isDefaultAddress: null,
    comefrom:'',
    userInfo: {},
    pIndex: 0,
    cIndex: 0,
    aIndex: 0,
    pArray: [],
    cArray: [],
    aArray: [],
    pValue: '',
    cValue: '',
    aValue: ''
  },
  setAddressStatus: function (e) {
    var that = this
    that.setData({
      isDefaultAddress: e.detail.value
    })
  },
  onLoad: function (options) {
    var that = this
    var comefrom = options.comefrom
    that.setData({
      comefrom: comefrom
    })
    that.refresh(0,0,0)
  },
  onUnload: function () {
    // Do something when page close.
    var that = this
    if (that.data.comefrom == '')
          wx.navigateBack({
              delta: 1
            })
  },
  refresh(pIndex,cIndex,aIndex){
    var that = this
    var pArray = utils.getProvince();
    var pIndex = pIndex
    var pValue = pArray[pIndex]

    var cArray = utils.getCity(pArray[pIndex])//得到市级的列表
    var cIndex = cIndex
    var cValue = cArray[cIndex]

    var aArray = utils.getArea(pValue, cValue)
    var aIndex = aIndex
    var aValue = aArray[aIndex]

    that.setData({
      pArray: pArray,
      pIndex: pIndex,
      pValue: pValue,
      cArray: cArray,
      cIndex: cIndex,
      cValue: cValue,
      aArray: aArray,
      aIndex: aIndex,
      aValue: aValue
    })

  },
  bindPickerChangeOfProvince: function (e) {
    var that = this
    var pIndex = e.detail.value//选择的省份编号
    that.refresh(pIndex, 0, 0)
  },

  bindPickerChangeOfCity: function (e) {
    var that = this
    var cIndex = e.detail.value
    that.refresh(that.data.pIndex, cIndex, 0)
  },
  bindPickerChangeOfArea: function (e) {
    var that = this
    var aIndex = e.detail.value
    that.refresh(that.data.pIndex, that.data.cIndex, aIndex)
  },
  formSubmit: function (e) {
    var that = this
    var host = app.globalData.host
    var isDefaultAddr = that.data.isDefaultAddress ? "1" : "0"
    var source = e.detail.value
    if (source.name && source.mobile && that.data.pValue && that.data.cValue && that.data.aValue && source.address && isDefaultAddr){
      var addressNum = wx.getStorageSync('addressNum')
      //如果这是第一次新增地址
      if (addressNum == 0){
        isDefaultAddr = "1"
      }

      wx.request({
        url: host + "/addressApi/address", //仅为示例，并非真实的接口地址
        data: {
          name: source.name,
          mobile: source.mobile,
          province: that.data.pValue,
          city: that.data.cValue,
          county: that.data.aValue,
          address: source.address,
          isDefault: isDefaultAddr,
          thirdpartyId: wx.getStorageSync('openId')
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          wx.showToast({
            title: '添加地址成功',
            icon: 'success',
            duration: 2000
          })
          // wx.navigateBack({
          //   delta: 1
          // })
          if(that.data.comefrom != '')
          wx.redirectTo({
            url: '../personaddress/personaddress?comefrom=' + that.data.comefrom
          })
          else {
            // wx.navigateBack({
            //   delta: 1
            // })
            wx.redirectTo({
              url: '../personaddress/personaddress'
            })
          }
            
        }
      })
    }else{
      wx.showToast({
        title: '请填写完整的地址信息',
        icon: 'success',
        duration: 2000
      })
    }    
  },
  formReset: function () {
  }
})